const app = getApp()
const globalInclude = require('../static/global')
const Utils = require('../utils/compile/utils')

let $global = globalInclude.$global

module.exports = Behavior({
  data: {
    departmentOptions: [],
    majorOptions: [],
    examOptions: [],
    schoolLinkData: {
      keys: [],
    },
  },
  methods: {
    schoolLinkSetOptions(type, query, options = {}) {
      let self = this
      return new Promise( (resolve, reject) => {
        if (!type) {
          reject(new Error())
        }
        let d = {
          label: type,
          options: [],
        }
        // console.d('Utils.isUndefined(options.value)', options.value, Utils.isUndefined(options.value))
        if (!Utils.isUndefined(options.value)) {
          d.value = options.value
        }
        if (!Utils.isUndefined(options.append)) {
          d.append = options.append
        }
        let ajaxTask
        if (type === 'department') {
          ajaxTask = $global.Api.getSchoolDepartment(query)
        }

        if (type === 'major') {
          ajaxTask = $global.Api.getDepartmentMajor(query)
        }

        if (type === 'exam') {
          ajaxTask = $global.Api.getCommonExamSubject(query)
        }

        ajaxTask.then(res => {
          if (options.response) {
            d.options = options.response(res)
          } else {
            if (!res.data) {
              res.data = []
            }
            if (type !== 'exam') {
              if (type === 'major') {
                d.options = res.data.map(v => {
                  v.label = `${v.name} (${v.code})`
                  v.value = v.uuid
                  return v
                })
              } else {
                d.options = res.data.map(v => {
                  v.label = v.name
                  v.value = v.uuid
                  return v
                })
              }
            }
            else {
              d.options = res.data.map(v => {
                v.label = v.name
                v.value = v.name
                return v
              })
            }
            console.d('schoolLinkSetOptions type', type, d)
          }
          self.schoolLinkSet(
            [
              d,
            ],
          ).then(() => {
            resolve({
              options: d.options
            })
          })
        })
      })
    },
    schoolLinkReciveModelKeys(...keys) {
      this.$set('schoolLinkData.keys', keys)
    },
    schoolLinkSet(types = []) {
      return new Promise((resolve, reject) => {
        let d = {}
        let keys = this.data.schoolLinkData.keys
        types.forEach(type => {
          d = Object.assign(d, type.append)
          if (type.label === 'department') {
            d.departmentOptions = type.options
            if (type.hasOwnProperty('value')) {
              d[keys[0]] = type.value
            }
          }
          if (type.label === 'major') {
            d.majorOptions = type.options
            if (type.hasOwnProperty('value')) {
              d[keys[1]] = type.value
            }
          }
          if (type.label === 'exam') {
            d.examOptions = type.options
            if (type.hasOwnProperty('value')) {
              d[keys[2]] = type.value
            }
          }
        })
        // console.d('schoolLinkSet', d)
        this.setData(d)
        wx.nextTick(() => {
          resolve()
        })
      })
    },
    schoolLinkClear(keys = []) {
      let d = keys.map(key => {
        return {
          label: key,
          options: [],
          value: null,
        }
      })
      this.schoolLinkSet(d)
    },
    schoolLinkClearOptions(keys = [], cb, append = {}) {
      let d = {}
      keys.forEach(key => {
        d[`${key}Options`] = []
      })
      d = Object.assign(d, append)
      // console.log('d', d)
      this.setData(d)
      wx.nextTick(cb)
    }
  },
})

